import requests
import simplejson
from openai import OpenAI

from model import Model

client = OpenAI(api_key="EMPTY", base_url="127.0.0.1:8000/v1")
class GLMModel(Model):
    def __init__(self, model_url: str, timeout: int):
        self.client = OpenAI(api_key="EMPTY", base_url="127.0.0.1:8000/v1")
        self.timeout = timeout

    def make_request(self, prompt):
        try:
            messages = [
                {
                    "role": "system",
                    "content": "你是一名翻译专家，请翻译我给你的内容，并按照我要求的格式返回",
                },
                {"role": "user", "content": prompt},
            ]

            response = client.chat.completions.create(
                model="chatglm3-6b", messages=messages
            )
            if response:
                translation = response.choices[0].message.content
                return translation, True
            else:
                print("Error:", response.status_code)

        except requests.exceptions.RequestException as e:
            raise Exception(f"请求异常：{e}")
        except requests.exceptions.Timeout as e:
            raise Exception(f"请求超时：{e}")
        except simplejson.errors.JSONDecodeError as e:
            raise Exception("Error: response is not valid JSON format.")
        except Exception as e:
            raise Exception(f"发生了未知错误：{e}")
        return "", False
